<script setup>
  import { ref } from 'vue'
  import { timeFormat } from '@/utils/util'
  import { getArticleById } from '@/api/article'
  import { getArticleByCommonById } from '@/api/common'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/stores'
  const router = useRouter()
  const userStore = useUserStore()
  const contentPopup = ref(null)
  const article = ref({})
  const loading = ref(false)
  function close() {
    contentPopup.value.close()
  }
  function open(id) {
    article.value = {}
    loading.value = true
    if (userStore.token) {
      getArticleById(id).then(res => {
        article.value = res.data
        loading.value = false
      })
    }else{
      getArticleByCommonById(id).then(res => {
        article.value = res.data
        loading.value = false
      })
    }
    contentPopup.value.open()
  }
  defineExpose({ open, close })
  function toEdit() {
    contentPopup.value.close()
    router.push({ path: '/post', query: { id: article.value.id } })
  }
</script>

<template>
  <Popup ref="contentPopup">
    <div class="popup" v-loading="loading">
      <div class="close" @click="close">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-quxiao"></use>
        </svg>
      </div>
      <div class="header flex align-center wrap">
        <div class="avatar flex align-center">
          <img src="../../assets/logo.png" />
          <span>Handsomedog</span>
        </div>
        <div class="right button" @click="toEdit" v-if="userStore.token">
          编辑
        </div>
      </div>
      <div class="content">
        <div class="title">
          {{ article.name }}
        </div>
        <div class="flex-between">
          <div class="tag">#{{ article.tag }}</div>
          <div class="time">
            <!-- 发布于 {{ timeFormat(article.createTime, "YYYY-mm-dd") }} -->
            最近更新于 {{ timeFormat(article.updateTime, 'YYYY-mm-dd HH:MM') }}
          </div>
        </div>
        <div class="html" v-html="article.content"></div>
      </div>
    </div>
  </Popup>
</template>

<style lang="scss" scoped>
  .popup {
    width: 60vw;
    height: calc(100vh - 60px);
    padding: 30px;
    border-radius: 30px;
    background: var(--color-background);
    z-index: 300;
    max-width: 1520px;
    .close {
      display: none;
      background: var(--color-background);
      cursor: pointer;
    }
    .header {
      margin-bottom: 20px;
      .button {
        line-height: 40px;
        height: 40px;
        font-size: 16px;
        width: 96px;
        transition: all 0.15s;
        margin-left: auto;
      }
      img {
        width: 40px;
        height: 40px;
        border-radius: 30%;
        margin-right: 10px;
      }
      span {
        font-size: 16px;
        color: var(--color-secondary-label);
        &:hover {
          color: var(--color-primary-label);
        }
      }
    }

    .content {
      color: var(--color-primary-label);
      height: calc(100% - 60px);
      overflow-y: scroll;
      .title {
        font-weight: 600;
        font-size: 18px;
        line-height: 140%;
      }
      .tag {
        color: var(--color-link);
      }
      .time {
        font-size: 14px;
        line-height: 120%;
        color: var(--color-tertiary-label);
      }
      .html {
        margin-top: 5px;
      }
    }
  }
  @media screen and (max-width: 695px) {
    .popup .close {
      display: table !important;
      margin-left: auto;
      margin-bottom: 10px;
    }
    .popup .popup {
      width: 100vw;
      height: 100vh;
      border-radius: 0;
      padding: 20px;
    }
  }
</style>
